/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jakewharton.rxbinding4.demo.slice;

import com.jakewharton.rxbinding4.ResourceTable;
import com.jakewharton.rxbinding4.component.RxComponent;
import com.jakewharton.rxbinding4.component.RxTabList;
import com.jakewharton.rxbinding4.component.RxText;
import com.jakewharton.rxbinding4.component.RxTickTimer;
import com.jakewharton.rxbinding4.component.RxTimePicker;
import com.jakewharton.rxbinding4.component.TextTextUpdateEvent;
import com.jakewharton.rxbinding4.component.TimePickerTimeChangeEvent;

import io.reactivex.rxjava3.functions.Consumer;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.TabList.Tab;
import ohos.agp.window.dialog.ToastDialog;

/**
 * SampleAbilitySlice
 *
 * @since 2021-1-1
 */
public class SampleAbilitySlice extends AbilitySlice {

    private TabList tl;
    private Text txt;
    private TickTimer tt;
    private TextField tf;
    private TimePicker tp;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_layout_sample);

        tl = (TabList) findComponentById(ResourceTable.Id_tl);
        txt = (Text) findComponentById(ResourceTable.Id_txt);
        tt = (TickTimer) findComponentById(ResourceTable.Id_tt);
        tf = (TextField) findComponentById(ResourceTable.Id_tf);
        tp = (TimePicker) findComponentById(ResourceTable.Id_tp);

        Tab tabA = tl.new Tab(this);
        tabA.setText("TAB-A");
        Tab tabB = tl.new Tab(this);
        tabB.setText("TAB-B");
        Tab tabC = tl.new Tab(this);
        tabC.setText("TAB-C");
        tl.addTab(tabA, true);
        tl.addTab(tabB);
        tl.addTab(tabC);
        tt.setBaseTime(900 * 1000);
        tt.setFormat("HH:mm:ss");
        tt.start();

        listener();
    }

    private void listener() {
        RxTabList.selections(tl).subscribe(
                        new Consumer<Tab>() {
                            @Override
                            public void accept(Tab tab) throws Throwable {
                                showToast(tab.getText() + " is selected");
                            }
                        });
        RxComponent.clicks(txt).subscribe(
                        new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Throwable {
                                RxTabList.select(tl).accept(2);
                            }
                        });
        RxText.textUpdateEvents(txt).subscribe(
                        new Consumer<TextTextUpdateEvent>() {
                            @Override
                            public void accept(TextTextUpdateEvent event) throws Throwable {
                                showToast("text updated:" + event.text());
                            }
                        });
        RxTickTimer.tickTimerUpdates(tt).subscribe(
                        new Consumer<TickTimer>() {
                            private int count;

                            @Override
                            public void accept(TickTimer tickTimer) throws Throwable {
                                if (count < 3) {
                                    count++;
                                    showToast("ticktimer updated:" + tickTimer.getText());
                                }
                            }
                        });
        RxText.editorActions(tf).subscribe(
                        new Consumer<Integer>() {
                            @Override
                            public void accept(Integer integer) throws Throwable {
                                showToast("editor action: " + integer);
                            }
                        });
        RxTimePicker.timeChangeEvents(tp).subscribe(
                        new Consumer<TimePickerTimeChangeEvent>() {
                            @Override
                            public void accept(TimePickerTimeChangeEvent event) throws Throwable {
                                showToast("hour=" + event.hour() + ", minute=" + event.minute() + ", second=" + event.second());
                            }
                        });
    }

    private void showToast(String content) {
        ToastDialog toast = new ToastDialog(this);
        toast.setText(content).show();
    }
}
